const comment = require("../models/comment.model");
const url = require("url");
var moment = require("moment");
exports.create = function (req, res, next) {
    console.log(req.body)
    const kitty = new comment(req.body);
    kitty.save().then((data) => {
        res.json(data)
    })
}
exports.update = function (req, res, next) {
    var id = req.params.id;
    console.log(req.params)
    comment.findByIdAndUpdate(id, { $set: req.body }, { new: false })
        .then(data => {
            res.json(data)
        })
}
exports.get = function (req, res, next) {
    var id = req.params.id;
    console.log(id)
    comment.findById(id).then(data => {
        res.json(data);
    })
}
exports.remove = function (req, res, next) {
    var id = req.params.id
    comment.findByIdAndRemove(id).then(data => {
        res.json(data);
    })
}
function reverseTree(data, pid) {
    var result = [];
    var temp = null;
    var data = JSON.parse(JSON.stringify(data));
    for (var i in data) {
        if (data[i].parentId == pid) {
            data[i].text = data[i].user + ":" + data[i].content;
            var a = data[i];
            result.push(data[i])
            temp = reverseTree(data, data[i]._id);
            if (temp.length > 0) {
                data[i].children = temp;
            }
        }
    }
    return result;
}
exports.list = function (req, res, next) {
    var page = req.body.page ? req.body.page : 1;
    var limit = req.body.rows ? req.body.rows : 10;
    comment.paginate({}, { page: +page, limit: +limit }, function (err, result) {
        var result = JSON.parse(JSON.stringify(result));
        result.rows = result.docs;
        delete result.docs;
        res.json(result)
    })
}
exports.newslist = function (req, res, next) {
    var newsid=req.params.newsid
    var page = req.body.page ? req.body.page : 1;
    var limit = req.body.rows ? req.body.rows : 10;
    console.log(newsid,"22222222")
    comment.paginate({newsid:newsid}, { page: +page, limit: +limit }, function (err, result) {
        var result = JSON.parse(JSON.stringify(result));
        result.rows = result.docs;
        delete result.docs;
        res.json(result)
    })
}
exports.lists = function (req, res, next) {
    var obj={};
    if (req.params.user!="[object Object]") {
        var user = req.params.user;
         userReg = new RegExp(user, "ig");
         obj={
             user:userReg
         }
    }
    var page = req.body.page ? req.body.page : 1;
    var limit = req.body.rows ? req.body.rows : 10;
    comment.paginate(obj, { page: +page, limit: +limit }, function (err, result) {
        var result = JSON.parse(JSON.stringify(result));
        result.rows = result.docs;
        delete result.docs;
        res.json(result)
    })
}
exports.listreply = function (req, res, next) {
    var id = req.params._id;
    comment.find().then(data=>{
        res.json(reverseTree(data,id));
    })
}
exports.removes = function (req, res, next) {
    var ids = req.body.ids;
    console.log(1111111111111111);
    console.log(ids)
    comment.remove({ _id: { $in: ids } }).then(data => {
        res.json({ "message": "删除多个记录成功" });
    })
};
exports.getreplaynum=function(req,res,next){
    var newid=req.params.id;
    comment.find({newsid:newid}).then(data=>{
        res.json(data.length)
    })
}